package cn.fsw.pethome.pet.service.impl;

import cn.fsw.pethome.base.service.impl.BaseServiceImpl;
import cn.fsw.pethome.order.domain.OrderPetAcquisition;
import cn.fsw.pethome.order.mapper.OrderPetAcquisitionMapper;
import cn.fsw.pethome.org.domain.Employee;
import cn.fsw.pethome.org.domain.Shop;
import cn.fsw.pethome.org.mapper.ShopMapper;
import cn.fsw.pethome.pet.domain.Pet;
import cn.fsw.pethome.pet.domain.SearchMasterMsg;
import cn.fsw.pethome.pet.domain.SearchMasterMsgAuditLog;
import cn.fsw.pethome.pet.domain.dto.MasterMsgDTO;
import cn.fsw.pethome.pet.mapper.PetMapper;
import cn.fsw.pethome.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.fsw.pethome.pet.mapper.SearchMasterMsgMapper;
import cn.fsw.pethome.pet.query.SearchMasterMsgQuery;
import cn.fsw.pethome.pet.service.ISearchMasterMsgService;
import cn.fsw.pethome.user.domain.User;
import cn.fsw.pethome.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fsw
 * @since 2022-10-26
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Override
    @Transactional
    public void add(SearchMasterMsg searchMasterMsg, User user) {
        /*参数校验*/
        if(StringUtils.isEmpty(searchMasterMsg.getTitle())){
            throw new BusinessException("标题不能为空");
        }
        if(StringUtils.isEmpty(searchMasterMsg.getName())){
            throw new BusinessException("宠物名不能为空");
        }
        if(StringUtils.isEmpty(searchMasterMsg.getAge())){
            throw new BusinessException("宠物年龄不能为空");
        }
        if(StringUtils.isEmpty(searchMasterMsg.getCoatColor())){
            throw new BusinessException("宠物颜色不能为空");
        }
        if(StringUtils.isEmpty(searchMasterMsg.getPrice())){
            throw new BusinessException("宠物价格不能为空");
        }
        if(StringUtils.isEmpty(searchMasterMsg.getPetType())){
            throw new BusinessException("宠物类型不能为空");
        }
        if(StringUtils.isEmpty(searchMasterMsg.getAddress())){
            throw new BusinessException("地址不能为空");
        }
        /*审核*/
        String text = searchMasterMsg.getTitle()+searchMasterMsg.getName()+"|"+
                searchMasterMsg.getCoatColor()+"|"+searchMasterMsg.getAddress();

        if(!BaiduAuditUtils.TextCensor(text)){
            throw new BusinessException("您填写的内容不合法,请重新填写");
        }
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        List<Shop> shops = shopMapper.selectAll();
        Shop shop = DistanceUtil.getNearestShop(point, shops);

        if(shop != null){
            searchMasterMsg.setShopId(shop.getId());
            System.out.println("你来生意了！赶快去接客！"+searchMasterMsg.getTitle());

        }
        searchMasterMsg.setUserId(user.getId());
        searchMasterMsg.setState(1);
        searchMasterMsg.setNote("审核通过");
        searchMasterMsgMapper.add(searchMasterMsg);



        SearchMasterMsgAuditLog searchMasterMsgAuditLog = new SearchMasterMsgAuditLog();
        searchMasterMsgAuditLog.setState(1);
        searchMasterMsgAuditLog.setNote("审核通过");
        searchMasterMsgAuditLog.setAuditTime(new Date());
        searchMasterMsgAuditLog.setMsgId(searchMasterMsg.getId());
        searchMasterMsgAuditLogMapper.add(searchMasterMsgAuditLog);

    }

    @Override
    public PageList<SearchMasterMsg> pageSelevtAll(SearchMasterMsgQuery query, Employee employee) {
        query.setState(1);
        if(employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        return super.pageSelevtAll(query);
    }

    /*接单*/
    @Override
    @Transactional
    public void accept(MasterMsgDTO dto) {
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.selectByMsgId(dto.getMsgId());
        if(orderPetAcquisition != null){
            throw new BusinessException("该订单已被接收");

        }
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.selectById(dto.getMsgId());
        Pet pet = gotoPet(searchMasterMsg);
        gotoOrder(dto, searchMasterMsg, pet);


    }
    /*拒单*/
    @Override
    public void reject(MasterMsgDTO dto) {
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.selectByMsgId(dto.getMsgId());
        if(orderPetAcquisition != null){
            throw new BusinessException("该订单已被接收,不能拒单");
        }
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.selectById(dto.getMsgId());
        searchMasterMsg.setShopId(null);
        searchMasterMsgMapper.update(searchMasterMsg);
    }

    private void gotoOrder(MasterMsgDTO dto, SearchMasterMsg searchMasterMsg, Pet pet) {
        OrderPetAcquisition orderPetAcquisition = new OrderPetAcquisition();
        orderPetAcquisition.setOrderSn(CodeGenerateUtils.generateOrderSn(pet.getId()));
        orderPetAcquisition.setDigest(dto.getNote());
        long lastTime = System.currentTimeMillis() + (30 * 1000 * 60);
        orderPetAcquisition.setLastcomfirmtime(new Date(lastTime));
        orderPetAcquisition.setState(1);
        BigDecimal bigDecimal = new BigDecimal(searchMasterMsg.getPrice());
        orderPetAcquisition.setPrice(bigDecimal);
        orderPetAcquisition.setPetId(pet.getId());
        orderPetAcquisition.setShopId(searchMasterMsg.getShopId());
        orderPetAcquisition.setUserId(searchMasterMsg.getUserId());
        orderPetAcquisition.setAddress(searchMasterMsg.getAddress());
        orderPetAcquisition.setEmpId(dto.getHandler());
        orderPetAcquisition.setSearchMasterMsgId(searchMasterMsg.getId());
        orderPetAcquisitionMapper.add(orderPetAcquisition);
    }

    private Pet gotoPet(SearchMasterMsg searchMasterMsg) {
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setCostprice(searchMasterMsg.getPrice().toString());
        pet.setTypeId(searchMasterMsg.getPetType());
        pet.setShopId(searchMasterMsg.getShopId());
        pet.setUserId(searchMasterMsg.getUserId());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        petMapper.add(pet);
        return pet;
    }
}
